#include "Appointment_t.h"

const Appointment_t& Appointment_t::invalid = Appointment_t(); // Invalid type

Appointment_t::Appointment_t()
	: m_startTime()
	, m_endTime()
	, m_subject("")
{
}

Appointment_t::~Appointment_t()
{
}

Appointment_t::Appointment_t(const Appointment_t& other)
	: m_startTime(other.m_startTime)
	, m_endTime(other.m_endTime)
	, m_subject(other.m_subject)
{
}

Appointment_t& Appointment_t::operator=(const Appointment_t& other)
{
	if(this != &other)
	{
		m_startTime = other.m_startTime;
		m_endTime = other.m_endTime;
		m_subject = other.m_subject;
	}

	return *this;
}

bool Appointment_t::operator==(const Appointment_t& other) const
{
	// Check for intersection 
	if(m_startTime.day() != other.m_startTime.day())
		return false;

	if(m_startTime <= other.m_startTime && other.m_startTime < m_endTime)
		return true;

	if(other.m_startTime <= m_startTime && m_startTime < other.m_endTime)
		return true;

	return false;
}

Appointment_t* Appointment_t::clone() const
{
	return new Appointment_t(*this);
}

bool Appointment_t::isValid() const
{
	return (this != &invalid);
}

bool Appointment_t::setTime(const DayTime_t& startTime, const DayTime_t& endTime)
{
	// Appointment has to start and end at the same day
	if(startTime.day() != endTime.day())
		return false;

	// Appointment must have a valid duration
	if(endTime <= startTime)
		return false;

	m_startTime = startTime;
	m_endTime = endTime;
	return true;
}

std::ostream& operator<<(std::ostream& output, const Appointment_t& app)
{
	const DayTime_t& start = app.startTime();
	const DayTime_t& end = app.endTime();
	const std::string& subject = app.getSubject();

	int startSize=(start.hours()<10)?1:0;
	int endSize=(end.hours()<10)?1:0;

	// output start time as xx:xx with following zero after the minutes
	output<<"|"<<start.hours()<<":";
	output.width(2);
	output.fill('0');
	output<<std::right;
	output<<start.minutes();

	// output end time as xx:xx with following zero after the minutes
	output<<"-"<<end.hours()<<":";
	output.width(2);
	output.fill('0');
	output<<std::right;
	output<<end.minutes();

	// spacing
	output<<std::left;
	output.width(4+startSize+endSize);
	output.fill(' ');
	output<<std::right;
	output<<"|";

	// output subject
	output.width(45);
	output.fill(' ');
	output<<std::left;
	output<<subject;
	output<<"|"<<std::endl;

	// output seperating line
	output.width(61);
	output.fill('-');
	output<<"|";
	output<<"|"<<std::endl;

	return output;
}